﻿//***********************************************************************
// Assembly         : NMortgage.UI
// Author           : Mike
// Created          : 04-29-2012
//
// Last Modified By : Mike
// Last Modified On : 04-21-2012
// Description      : 
//
// Copyright        : (c) . All rights reserved.
//***********************************************************************
using System.Globalization;
namespace NMortgage.UI.Model
{
    using RAND = Troschuetz.Random;

    public sealed class NormalDistribution : ABaseValidatedType<NormalDistribution, NormalDistributionValidator>, IDistribution
    {
        #region Fields
        private const double DEFAULT_MEAN = 4.1d;
        private const double DEFAULT_DEV = 1.0d;

        private RAND.NormalDistribution distribution;
        private RAND.Generator generator;
        private double mean;
        private double stdDev;

        #endregion Fields

        #region Constructors

        public NormalDistribution()
        {
            generator = new RAND.StandardGenerator();
            distribution = new RAND.NormalDistribution(generator);

            Mean = DEFAULT_MEAN;
            StdDev = DEFAULT_DEV;
        }

        #endregion Constructors

        #region Properties

        public string Description
        {
            get
            {
                return string.Format(CultureInfo.CurrentCulture, Properties.Resources.NormalRateRateDescriptionFormat, Mean, StdDev);
            }
        }

        public double Mean
        {
            get { return mean; }
            set
            {
                mean = value;
                distribution.Mu = Mean;
                NotifyOfPropertyChange(() => Mean);
                NotifyOfPropertyChange(() => StdDev);
            }
        }

        public double StdDev
        {
            get { return stdDev; }
            set
            {
                stdDev = value;
                distribution.Sigma = StdDev;
                NotifyOfPropertyChange(() => Mean);
                NotifyOfPropertyChange(() => StdDev);
            }
        }

        #endregion Properties

        #region Methods

        public double GetRate()
        {
            return ScaleRandomToPercentRange(distribution.NextDouble());
        }

        private double ScaleRandomToPercentRange(double randomNummber)
        {
            return System.Math.Abs(randomNummber / 100d);
        }

        #endregion Methods
    }
}